//
//  DestOtherCellInfoViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestOtherCellInfoViewController.h"
#import "DestAFNDataBlock.h"
#import "BaseHeader.h"
#import "DestTabBarFourImageCollectionViewCell.h"
#import "DestHeaderCollectionReusableView.h"
#import "DestTabBarModel.h"
#import "DestFooterCollectionReusableView.h"
#import "DestFirstCellInfoViewController.h"
#import "DestAllHotCityViewController.h"
#import "DestPhotoesViewController.h"
#import "GiFHUD.h"
@interface DestOtherCellInfoViewController ()<UIWebViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(strong,nonatomic)NSString *urlString;///webView的网址的字符串
@property(strong,nonatomic)UICollectionView *destTabBarCollectionView;
@property(strong,nonatomic)UICollectionViewFlowLayout *destFlowLayout;
@property (strong,nonatomic)NSMutableArray *DestcellModelArray;
@property(strong,nonatomic)NSString *hottest_placesStr;//头部大图的网址
@property (strong,nonatomic)NSString *goOrWantToGoStr;
@property (strong,nonatomic)NSString *nameStr;
@property(strong,nonatomic)NSNumber *handingTypeStr;///判断类型的 推荐城市  或者热门地点
@property(strong,nonatomic)NSNumber *allHotCityType;

@property(strong,nonatomic)NSNumber *photoesId;
@end

@implementation DestOtherCellInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake(0, 20, 25, 25);
    [button setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    //初始化数组
    _DestcellModelArray  = [[NSMutableArray alloc]init];
    [self getData];
    ///创建 collectionView的方法
    [self createCollectionView];
    [self createBackButton];
}
#pragma mark -  创建返回按钮
- (void)createBackButton {
    UIButton *backButton =[UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    backButton.frame =CGRectMake(5, 20, 30, 30);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}
#pragma mark - 创建 collectionView的方法
- (void)createCollectionView {
    _destFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    _destFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH -25 *WIDTH)/2,(SCREEN_WIDTH -25 *WIDTH)/2 );
    _destFlowLayout.minimumInteritemSpacing = 5 *WIDTH;
    _destFlowLayout.minimumLineSpacing = 5 *HEIGHT;
    _destFlowLayout.headerReferenceSize = CGSizeMake(101, 400 *HEIGHT);
    _destFlowLayout.sectionInset = UIEdgeInsetsMake(10 * HEIGHT, 10 * WIDTH, 10 * HEIGHT, 10 *WIDTH);
    _destTabBarCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) collectionViewLayout:_destFlowLayout];
    _destTabBarCollectionView.delegate = self;
    _destTabBarCollectionView.dataSource =self;
    _destTabBarCollectionView.showsHorizontalScrollIndicator = NO;
    _destTabBarCollectionView.backgroundColor = [UIColor whiteColor];
    [_destTabBarCollectionView registerClass:[DestTabBarFourImageCollectionViewCell class] forCellWithReuseIdentifier:@"destTabBarFourImageCollectionViewCell"];
    //注册头部
    [_destTabBarCollectionView registerClass:[DestHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"destHeaderCollectionReusableView"];
    //注册footer
    [_destTabBarCollectionView registerClass:[DestFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"destFooterCollectionReusableView"];
    [self.view addSubview:_destTabBarCollectionView];
    //设置多选
    _destTabBarCollectionView.allowsMultipleSelection = YES;
}
#pragma mark - 返回section头的高度的代理方法
- (CGSize) collectionView: (UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 390*HEIGHT);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    //进来 数据没有加载的时候 不让显示
    if (_handingTypeStr == nil) {
        return CGSizeZero;
        
    }else{
        
        if (_handingTypeStr.integerValue ==1) {
            return CGSizeZero;
            
        }else if ([_handingTypeStr isKindOfClass:[NSNull class]]){
            return CGSizeZero;
            
        }else{
            return CGSizeMake(SCREEN_WIDTH, 50 *HEIGHT);
        }
    }
}
#pragma mark - 返回cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _DestcellModelArray.count;
}
#pragma mark -  返回cell种类
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DestTabBarFourImageCollectionViewCell *destcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destTabBarFourImageCollectionViewCell" forIndexPath:indexPath];
    DestTabBarModel *model = [_DestcellModelArray objectAtIndex:indexPath.row];
    destcell.destModel = model;
    return destcell;
}
#pragma mark  -返回头部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        DestHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"destHeaderCollectionReusableView" forIndexPath:indexPath];
        [headerView.destHeaderImageView sd_setImageWithURL:[NSURL URLWithString:_hottest_placesStr] placeholderImage:PLACEHOLDERIMAGE];
        headerView.destNameLabel.text = _nameStr;
        headerView.destGoOrWantToGoLabel.text = _goOrWantToGoStr;
        if (_hottest_placesStr == nil) {
            
        }else{
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imagetap)];
            headerView.destHeaderImageView.userInteractionEnabled = YES;
            
            [headerView.destHeaderImageView addGestureRecognizer:tap];
        }
        if (_handingTypeStr.integerValue == 1) {
            headerView.hotLabel.text = @"推荐城市";
            
        }else if (_handingTypeStr.integerValue == 5){
            headerView.hotLabel.text = @"热门地点";
        }else if (_handingTypeStr.integerValue == 2){
            headerView.hotLabel.text = @"热门地点";
        }else if (_handingTypeStr.integerValue ==3 ){
            headerView.hotLabel.text = @"热门地点";
        }else if (_handingTypeStr.integerValue == 4){
            headerView.hotLabel.text = @"热门地点";
        }
        
        
        return headerView;
    }else {
        DestFooterCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"destFooterCollectionReusableView" forIndexPath:indexPath];
        [footerView.Morebutton addTarget:self action:@selector(MorebuttonClick) forControlEvents:UIControlEventTouchUpInside];
        return footerView;
    }
    
}

- (void)MorebuttonClick {
    
    DestAllHotCityViewController * allHotCity = [[DestAllHotCityViewController alloc] init];
    allHotCity.type = _allHotCityType;
    allHotCity.myId = _MyId;
    [self.navigationController pushViewController:allHotCity animated:YES];
    
}
#pragma mark -请求数据
- (void)getData{
    ///皮卡丘
    
    
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",_type,_MyId] AFNData:^(id block) {
        
        _photoesId = [block objectForKey:@"id"];
        _allHotCityType = [block objectForKey:@"type"];
        
        NSMutableDictionary *dic = [block objectForKey:@"share_args"];
        
        NSMutableDictionary *defaultDic =[dic objectForKey:@"default"];
        //        NSLog(@"%@",block);
        _urlString = [defaultDic objectForKey:@"shr_url"];
        //collection头部赋值的语句
        NSMutableArray *hottest_placesArray = [block objectForKey:@"hottest_places"];
        _hottest_placesStr = [[hottest_placesArray objectAtIndex:0]objectForKey:@"photo"];
        _nameStr = [block objectForKey:@"name"];
        NSNumber *goNumber = [block objectForKey:@"visited_count"];
        NSNumber *wantToGo = [block objectForKey:@"wish_to_go_count"];
        _goOrWantToGoStr = [NSString stringWithFormat:@"%@去过 /%@ 喜欢",goNumber,wantToGo];
        _handingTypeStr  =[block objectForKey:@"handlingtype"];
        //        NSLog(@"hangdingType===%@",_handingTypeStr);
        //给colllection赋值
        NSMutableArray *hottest_sitesArray = [block objectForKey:@"hottest_sites"];
        for (NSMutableDictionary *hottestDic in hottest_sitesArray) {
            DestTabBarModel *model = [DestTabBarModel modelWithDictionary:hottestDic];
            [_DestcellModelArray addObject:model];
        }

        ///刷新
        [_destTabBarCollectionView reloadData];
        
    } AFNData1:^(id block) {

    }];
}
#pragma mark - 返回上一页面的方法
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_handingTypeStr.integerValue == 1) {
        DestOtherCellInfoViewController *destother = [[DestOtherCellInfoViewController alloc]init];
        DestTabBarModel *model = [_DestcellModelArray objectAtIndex:indexPath.row];
        destother.type = model.type;
        destother.MyId = model.MyId;
        [self.navigationController pushViewController:destother animated:YES];
        
        
    }else{
        DestFirstCellInfoViewController *first = [[DestFirstCellInfoViewController alloc]init];
        DestTabBarModel *model = [_DestcellModelArray objectAtIndex:indexPath.row];
        first.type = model.type;
        first.MyId = model.MyId;
        [self.navigationController pushViewController:first animated:YES];
        
    }
}
#pragma mark - 视图即将出现
-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden  = YES;
    self.navigationController.navigationBarHidden = YES;
}
#pragma mark - 返回
- (void)DestGoBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)imagetap {
    DestPhotoesViewController *photoes = [[DestPhotoesViewController alloc]init];
    photoes.MyId = _photoesId;
    photoes.myType = _allHotCityType;
    photoes.cityName = _nameStr;
    [self.navigationController pushViewController:photoes animated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
